/* 1996. 5.

BOARD.CPP


*/
#include <windows.h>
#include "public.h"
#include "board.h"


/* PRIVATE AREA */
void BOARD::Line(HDC hdc, int left, int top, int right, int bottom)
{
	MoveToEx(hdc,left,top,NULL);
	LineTo(hdc,right,bottom);
}


/* PUBLIC AREA */
BOARD::BOARD(int icxSize, int icySize, int ixSize, int iySize)
{
	cxSize = icxSize;
	cySize = icySize; 
	Resize(ixSize, iySize);
	Clear();
}                          
int BOARD::Getx(int cx){return (cx+1)*xTab;}
int BOARD::Gety(int cy){return (cy+1)*yTab;} 
int BOARD::Getcx(int x){return (x+xTab/2)/xTab -1 ;}
int BOARD::Getcy(int y){return (y+yTab/2)/yTab -1 ;}
void BOARD::Resize(int ix, int iy)
{
	xTab = ix / ( cxSize + 1 ) ;
	yTab = iy / ( cySize + 1 ) ; 
}



void BOARD::Clear(void)
{
	int x, y;
	for(x=0;x<=cxSize;x++) {
		for(y=0;y<=cySize;y++) {
			tbl[x][y]=VACANT;
		}
	}              
}
BOOL BOARD::Put(HDC hdc, int icx, int icy, int color)
{
	if(tbl[icx][icy]) return FALSE; 
	if(icx<0 || icy<0 || icx>=cxSize || icy>=cySize) return FALSE ;
	else tbl[icx][icy]=color;
	DrawStone(hdc,icx,icy);
	return TRUE;
}
int BOARD::Get(int ix,int iy)
{                          
	if(ix<0 || iy<0 || ix>=cxSize || iy>=cySize) return -1;
	return tbl[ix][iy];
}
void BOARD::DrawGrid(HDC hdc)
{                          
	int x, y;
	for(y=0;y<cySize;y++) {  
		Line(hdc,Getx(0),Gety(y),Getx(cxSize-1),Gety(y));
	}
	for(x=0;x<cxSize;x++) {  
		Line(hdc,Getx(x),Gety(0),Getx(x),Gety(cySize-1));
	}
}

void BOARD::DrawAllStone(HDC hdc)
{
	int x, y;
	for(x=0;x<cxSize;x++) {
		for(y=0;y<cySize;y++) {
			DrawStone(hdc, x, y);
		}
	}
}

void BOARD::DrawStone(HDC hdc, int icx, int icy)
{                                                
	HBRUSH hbrush;       
	if(tbl[icx][icy]==WHITE) hbrush=(HBRUSH)GetStockObject(NULL);
	else if(tbl[icx][icy]==BLACK) hbrush=(HBRUSH)GetStockObject(GRAY_BRUSH);
	else return ;
	SelectObject(hdc,hbrush);
	Ellipse(hdc,Getx(icx)-xTab/2,Gety(icy)-yTab/2,Getx(icx)+xTab/2,Gety(icy)+yTab/2);
	DeleteObject(hbrush);
}
